/*
 * LZXMethodTest.java July 2009
 *
 * Copyright (C) 2009, Quirino Zagarese <quirino@laszloitalia.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 */
package org.eu4ria.tools.lzstubs;

import java.util.jar.JarFile;

import org.eu4ria.AppAction;

public class LZXMethodTest {

	private LZXTemplate template;

	@SuppressWarnings("unchecked")
	public LZXMethodTest(AppAction action, JarFile jarFile) throws Exception {
		template = new LZXTemplate("methodtesttemplate.lzx", jarFile
				.getInputStream(jarFile.getEntry("methodtesttemplate.lzx")));
		template.setAttribute("METHODNAME", action.getJavaMethod().getName());
		template.setAttribute("MODULENAME", action.getDeclaringModule()
				.getJavaClass().getSimpleName());
		String paramsFormContent = "";
		String argsContent = "";
		Class[] types = action.getJavaMethod().getParameterTypes();
		int paramsNum = types.length;
		for (int i = 0; i < paramsNum; i++) {
			if (types[i].isArray()) {
				LZXTemplate param = new LZXTemplate("arrayinputtemplate.lzx",
						jarFile.getInputStream(jarFile
								.getEntry("arrayinputtemplate.lzx")));
				param.setAttribute("NAME", "p" + i);
				paramsFormContent = paramsFormContent + "\n"
						+ param.getResult();
				argsContent = argsContent + "parent.p" + i + ".getValue(),";
			} else {
				LZXTemplate param = new LZXTemplate("simpleinputtemplate.lzx",
						jarFile.getInputStream(jarFile
								.getEntry("simpleinputtemplate.lzx")));
				param.setAttribute("NAME", "p" + i);
				paramsFormContent = paramsFormContent + "\n"
						+ param.getResult();
				argsContent = argsContent + "parent.p" + i + "['text'],";
			}
		}
		template.setAttribute("PARAMSFORM", paramsFormContent);
		template.setAttribute("ARGS", argsContent);
	}

	public String serialize() {
		return template.getResult();
	}

}
